package com.cmq.web_1.api_test;

public class ApiResponse<T> {
    private boolean success;     // 请求是否成功
    private String message;      // 响应消息
    private int code;           // 状态码
    private T data;             // 响应数据
    private long timestamp;     // 时间戳

    // 构造方法
    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResponse(boolean success, String message, int code, T data) {
        this();
        this.success = success;
        this.message = message;
        this.code = code;
        this.data = data;
    }

    // 成功响应的静态方法
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, "操作成功", 200, data);
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(true, message, 200, data);
    }

    // 错误响应的静态方法
    public static <T> ApiResponse<T> error(String message, int code) {
        return new ApiResponse<>(false, message, code, null);
    }

    public static <T> ApiResponse<T> error(String message) {
        return new ApiResponse<>(false, message, 500, null);
    }

    // Getter 和 Setter 方法
    public boolean isSuccess() { return success; }
    public void setSuccess(boolean success) { this.success = success; }

    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }

    public int getCode() { return code; }
    public void setCode(int code) { this.code = code; }

    public T getData() { return data; }
    public void setData(T data) { this.data = data; }

    public long getTimestamp() { return timestamp; }
    public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
}